package com.power.weboffice.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.power.sifa.approve.model.Aprv_process_materials;
import com.power.sifa.approve.service.Aprv_materials_optionsService;
import com.power.sifa.approve.service.Aprv_process_materialsService;
import com.power.weboffice.dao.WebOfficeCallbackDao;
import com.power.weboffice.service.WebOfficeCallbackService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class WebOfficeCallbackServiceImpl implements WebOfficeCallbackService {

    @Resource
    private WebOfficeCallbackDao webOfficeCallbackDao;

    @Resource
    private Aprv_process_materialsService aprv_process_materialsService;

    @Resource
    private Aprv_materials_optionsService aprv_materials_optionsService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveCallback(String userid, String applyid, String attachid) {
        // 根据文件路径查询attachid
        try {
            // 备份材料历史记录
            List<Aprv_process_materials> materials = this.aprv_process_materialsService.getAllMaterialByAttachId(attachid);
            if (CollectionUtil.isNotEmpty(materials)) {
                this.aprv_materials_optionsService.initMaterialOptions(materials.get(0), userid);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("saveCallback 备份材料历史记录失败 ->  msg[{}]", e.getMessage());
            throw new RuntimeException("备份材料历史记录失败！");
        }
    }
}
